$(function(){
	//初始化表格
	
	$("#dataGird").flexigrid({
		url:"/system/admin/role/selectForPage",
		colModel:[
		          {display:"ID",name:"id",width:80,sortable:true,hide: true},
		          {display:"名称",name:"name",width:80,sortable:true},
		          {display:"描述",name:"description",width:80,sortable:true},
		          {display:"创建人",name:"creator",width:80,sortable:true},
		          {display:"创建时间",name:"gmtCreate",width:80,sortable:true},
		          {display:"修改人",name:"modifier",width:80,sortable:true},
		          {display:"修改时间",name:"gmtModify",width:80,sortable:true}
		],
		onSelectRow: true
	});
	
	
	//jstree
	$("#menuTree").jstree({
		"json_data" : {
			"ajax" : {
				"url" : "/system/admin/menu/children.json",
				"data" : function(n) {
					return {
						id : n.attr ? n.attr("menuId") : 0
					};
				}
			}
		},
		"plugins" : [ "themes", "json_data", "checkbox" ]
	});

	$("#clearButton").bind("click",function(){
		MZ.clearForm("searchForm");
	});
	
	$("#searchButton").bind("click",function(){
		MZ.searchGird("searchForm","dataGird");
	});
	
	//保存
	$("#save").bind("click",function(){
		var menus=$("#menuTree").jstree("get_checked",null, true);
		var menuIds=new Array();
		for(var i=0;i<menus.length;i++){
			menuIds.push($(menus[i]).attr("menuid"));
		}

		var id=$("#roleId").val();
		if	(id==""){
			MZ.showMsg("请先选择要修改的数据");
			return;
		}
		var name=$("#nameInput").val();
		if ($.trim(name)==""){
			MZ.showMsg("角色名称不能为空");
			return;
		}
		var data={name:name,menuIds:menuIds,id:id};
		MZ.ajaxShowMsg({
			url : "/system/admin/role/save",
			data:data,
			success:function(){
				MZ.searchGird("searchForm","dataGird");	
			}
		});
	});
	
	//新增
	$("#new").bind("click",function(){
		var menus=$("#menuTree").jstree("get_checked",null, true);
		var menuIds=new Array();
		for(var i=0;i<menus.length;i++){
			menuIds.push($(menus[i]).attr("menuid"));
		}
		var name=$("#nameInput").val();
		if ($.trim(name)==""){
			MZ.showMsg("角色名称不能为空");
			return;
		}
		var id=$("#roleId").val();
		var data={name:name,menuIds:menuIds};
		MZ.ajaxShowMsg({
			url : "/system/admin/role/save",
			data:data,
			success:function(){
				MZ.searchGird("searchForm","dataGird");	
			}
		});
	});
	
	//删除
	$("#del").bind("click",function(){
		if ($("#dataGird").flexigrid().getRowValue(2).length<=0){
			MZ.showMsg("请选择要删除的数据");
			return ;
		}
		MZ.ajaxShowMsg({
			url : "/system/admin/role/delete",
			data:{id:$("#dataGird").flexigrid().getRowValue(2)}
		});
		MZ.searchGird("searchForm","dataGird");
	});
});

//funciton onSelectRow required
//update flexigrid onselectrow function
//if you need't use onselect row
//let this function blank to do nothing
function onSelectRow(tr){
	$.post("/system/admin/role/selectById",{id:tr.id},function(result){
//		alert(result.data.name);
		$("#nameInput").val(result.data.name);
		$("#roleId").val(result.data.id);
		$("#menuTree").jstree("uncheck_all");
		var auths;
		try{
			auths=result.data.authorization.split(";")[0].replace("menu:","").split(",");
		}catch(e){
			
		}
//		change_state ( node , uncheck )
//		alert(auths);
		if (auths.length>0){
			var a=$("#menuTree li[menuid]");
			$.each($("#menuTree li[menuid]"), function(i, node){
//				if (node.attr("menuid")==1){
				var postion=$(node).attr("menuid");
				var segment = Math.floor(postion / 64);
				var offset = (postion % 64);
				segment = (offset == 0 ? segment - 1 : segment);
				offset = (offset == 0 ? 64 : offset);
				if (segment>auths.length){
					return;
				}else if ((parseInt(auths[segment])& Math.pow(2,(offset-1)))!==0){
					$("#menuTree").jstree("change_state",node,false);
				}
			});
//			for(var i=0;i<auths.length;i++){
//				for (var i=0;i<64;i++){
//					if (){
//						
//					}
//				}
//			}
		}
	});
//	alert(tr.id);
//	alert(tr.name);
}